#ifndef SESSION_H
#define SESSION_H

#include <QObject>
#include <QTcpSocket>
#include "Editor/editor.h"

class DocumentManager;
class UserManager;
class ChatManager;
class Server;

class Session : public QObject
{
Q_OBJECT

public:
	Session();

	//Networking Operations
	bool connectToServer(QString address, int port);	//connect to a server
	void runAsServer(QString newSessionName, int port);
	void leaveSession() {};
	bool isConnectedToServer() {return (connection != NULL);}
	bool isServer() {return (server != NULL);}

	//Saving and Loading Sessions
	QString getSessionFileName() {return sessionFileName;}
	void saveSession(QString filename);
	void saveSession(QByteArray&);
        void loadSession(QString filename);
	void loadSession(QByteArray&);

	//Message Sending
	void sendMessage(QByteArray data);

	//Get Managers
	DocumentManager *getDocumentManager() {return documentManager;}
	UserManager *getUserManager() {return userManager;}
	ChatManager *getChatManager() {return chatManager;}

private slots:
	void recieveMessage();

private:
	void handleMessage(QByteArray msg);
	QByteArray getMessageBlocking();

	QTcpSocket *connection;
	QString sessionFileName;
	QString sessionName;
	DocumentManager *documentManager;
	UserManager *userManager;
	ChatManager *chatManager;
	Server *server;

	qint64 currentMessageSize;	//the size of the current message we are waiting to receieve

	const static int timeout;
};

#endif // SESSION_H
